package DataBase;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Vector;

import DataObjects.RecipeRightList;

public class AddNewRecipeToDataBase {
	
	private Vector<RecipeRightList> recipeDetails;
	private Vector<String> recipeDetails1;
	private int i=0;
	private int status=0;
	
	


	public AddNewRecipeToDataBase(Vector<RecipeRightList> recipeDetail,Vector<String> recipeDetail1)
	{
		
		this.recipeDetails=recipeDetail;
		this.recipeDetails1=recipeDetail1;
		
	}
	

public void saveNewRecipe()
{
	SQLConnection sc=new SQLConnection();
	try {
		
			
		
		PreparedStatement ps2 = sc.getConnection().prepareStatement("INSERT INTO recipe (Rid,Rname,rconstPrice,Rstatus,Rmishkal) values(?,?,?,?,?);");	
		
		sc.getConnection().setAutoCommit(false);
		ps2.setString(1,(String) recipeDetails1.get(0));
		ps2.setString(2,(String) recipeDetails1.get(1));
		ps2.setString(3,(String) recipeDetails1.get(2));     
		ps2.setString(4,(String) recipeDetails1.get(3));
		ps2.setString(5,(String) recipeDetails1.get(4));
		ps2.executeUpdate();
		sc.getConnection().commit();
		
		ps2.close();
		while(i<recipeDetails.size())
		{
			
			
			PreparedStatement ps1 = sc.getConnection().prepareStatement("INSERT INTO item_in_recipe (Rid,Iid,Iquantity) values(?,?,?);");	
			sc.getConnection().setAutoCommit(false);
			ps1.setString(1,(String) recipeDetails1.get(0));
			ps1.setInt(2, recipeDetails.get(i).getItemID());
			ps1.setFloat(3, recipeDetails.get(i).getiQuantity());
			ps1.executeUpdate();
			sc.getConnection().commit();
			
			status=1;
			ps1.close();
			i++;
		}
		sc.getConnection().close();
							
	}
	
	catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		
		
		
	}
	}
	
             public int getStatus() {
	               return status;
              }


             public void setStatus(int status) {
	              this.status = status;
              }

}
